/********************************************************************************
 * Project Name		[JavaSE_Fundamentals]
 * File Name     	[ThreadClock.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package thread;

import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JFrame;
import javax.swing.JLabel;

/**
 * 线程时钟
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
public class ThreadClock {

	/**
	 * 线程时钟 - 使用Thread.sleep();
	 * 
	 * @author 不落的太阳(Sean Yang aka ShortPeace)
	 * @version 1.0
	 * @since jdk 1.8
	 *
	 */
	static class ThreadClock1 {
		public void initClock() {
			JFrame frame = new JFrame("我的时钟");
			JLabel clock = new JLabel("clock");
			clock.setHorizontalAlignment(JLabel.CENTER);
			frame.add(clock, "Center");
			frame.setSize(240, 80);
			frame.setLocation(300, 300);
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			frame.setVisible(true);

			while (true) {
				clock.setText(getTime());
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}

		private String getTime() {
			Calendar calendar = Calendar.getInstance();
			String time = calendar.get(Calendar.YEAR) + "年" + (calendar.get(Calendar.MONTH) + 1) + "月"
					+ calendar.get(Calendar.DATE) + "日 ";

			int h = calendar.get(Calendar.HOUR_OF_DAY);
			int m = calendar.get(Calendar.MINUTE);
			int s = calendar.get(Calendar.SECOND);

			String ph = h < 10 ? "0" : "";
			String pm = m < 10 ? "0" : "";
			String ps = s < 10 ? "0" : "";
			return time + ph + h + "时" + pm + m + "分" + ps + s + "秒";
		}
	}

	/**
	 * 线程时钟 - 使用java.util.Timer
	 * 
	 * @author 不落的太阳(Sean Yang aka ShortPeace)
	 * @version 1.0
	 * @since jdk 1.8
	 *
	 */
	static class ThreadClock2 {

		class MyTimerTask extends TimerTask {

			private JLabel clock;

			MyTimerTask(JLabel clock) {
				super();
				this.clock = clock;
			}

			@Override
			public void run() {
				clock.setText(getTime());
			}

			private String getTime() {
				Calendar calendar = Calendar.getInstance();
				String time = calendar.get(Calendar.YEAR) + "年" + (calendar.get(Calendar.MONTH) + 1) + "月"
						+ calendar.get(Calendar.DATE) + "日 ";

				int h = calendar.get(Calendar.HOUR_OF_DAY);
				int m = calendar.get(Calendar.MINUTE);
				int s = calendar.get(Calendar.SECOND);

				String ph = h < 10 ? "0" : "";
				String pm = m < 10 ? "0" : "";
				String ps = s < 10 ? "0" : "";
				return time + ph + h + "时" + pm + m + "分" + ps + s + "秒";
			}

		}

		public void initClock() {
			JFrame frame = new JFrame("我的时钟");
			JLabel clock = new JLabel("clock");
			clock.setHorizontalAlignment(JLabel.CENTER);
			frame.add(clock, "Center");
			frame.setSize(240, 80);
			frame.setLocation(300, 300);
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			frame.setVisible(true);

			Timer timer = new Timer();
			TimerTask task = new MyTimerTask(clock);
			timer.schedule(task, 0, 1000);
		}
	}

	public static void main(String[] args) {
		// new ThreadClock1().initClock();
		new ThreadClock2().initClock();
	}
}
